﻿/*
///    This file is part of BannerLib4Net.
///
///    BannerLib4Net is free software: you can redistribute it and/or modify
///    it under the terms of the GNU General Public License as published by
///    the Free Software Foundation, either version 3 of the License, or
///    (at your option) any later version.

///    BannerLib4Net is distributed in the hope that it will be useful,
///    but WITHOUT ANY WARRANTY; without even the implied warranty of
///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
///    GNU General Public License for more details.
///
///    You should have received a copy of the GNU General Public License
///    along with BannerLib4Net.  If not, see <http://www.gnu.org/licenses/>.
*/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BannerLib4Net.Data.Validation;
using Npgsql;
using NpgsqlTypes;

namespace BannerLib4Net.Postgresql.Extensions
{
    public static class DataCommandExtensions
    {
        public static void AddVarcharParameter(this NpgsqlCommand cmd, String parameterName, String parameterValue, int size = 255)
        {
            NpgsqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.NpgsqlDbType = NpgsqlDbType.Varchar; 
            prm.Value = DataInputValidator.CheckInputStringValue(parameterValue);
            prm.Size = size;

            cmd.Parameters.Add(prm);

            
        }

        public static void AddInt16Parameter(this NpgsqlCommand cmd, String parameterName, Int16 parameterValue)
        {
            NpgsqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.NpgsqlDbType = NpgsqlDbType.Smallint;
            prm.Value = DataInputValidator.CheckInputInt16Value(parameterValue);
            cmd.Parameters.Add(prm);
        }

        public static void AddInt32Parameter(this NpgsqlCommand cmd, String parameterName, Int32 parameterValue)
        {
            NpgsqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.NpgsqlDbType = NpgsqlDbType.Integer;
            prm.Value = DataInputValidator.CheckInputInt32Value(parameterValue);
            cmd.Parameters.Add(prm);
        }

        public static void AddInt32Parameter(this NpgsqlCommand cmd, String parameterName, Int32? parameterValue)
        {
            NpgsqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.NpgsqlDbType = NpgsqlDbType.Integer;
            prm.Value = DataInputValidator.CheckInputInt32Value(parameterValue);
            cmd.Parameters.Add(prm);
        }

        public static void AddInt64Parameter(this NpgsqlCommand cmd, String parameterName, Int64 parameterValue)
        {
            NpgsqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.NpgsqlDbType = NpgsqlDbType.Bigint;
            prm.Value = DataInputValidator.CheckInputInt64Value(parameterValue);
            cmd.Parameters.Add(prm);
        }

        public static void AddInt64Parameter(this NpgsqlCommand cmd, String parameterName, Int64? parameterValue)
        {
            NpgsqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.NpgsqlDbType = NpgsqlDbType.Bigint;
            prm.Value = DataInputValidator.CheckInputInt64Value(parameterValue);
            cmd.Parameters.Add(prm);
        }


        public static void AddBooleanParameter(this NpgsqlCommand cmd, String parameterName, bool parameterValue)
        {
            NpgsqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.NpgsqlDbType = NpgsqlDbType.Boolean;
            prm.Value = parameterValue;
            cmd.Parameters.Add(prm);
        }

        public static void AddDecimalParameter(this NpgsqlCommand cmd, String parameterName, decimal parameterValue)
        {
            NpgsqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.NpgsqlDbType = NpgsqlDbType.Numeric;
            prm.Value = DataInputValidator.CheckInputDecimalValue(parameterValue);
            cmd.Parameters.Add(prm);
        }

        public static void AddDoubleParameter(this NpgsqlCommand cmd, String parameterName, double parameterValue)
        {
            NpgsqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.NpgsqlDbType = NpgsqlDbType.Double;
            prm.Value = DataInputValidator.CheckInputDoubleValue(parameterValue);
            cmd.Parameters.Add(prm);
        }


        public static void AddTimestampParameter(this NpgsqlCommand cmd, String parameterName, DateTime parameterValue)
        {
            NpgsqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.NpgsqlDbType = NpgsqlDbType.Timestamp;
            prm.Value = DataInputValidator.CheckInputDateTimeValue(parameterValue);
            cmd.Parameters.Add(prm);
        }

        public static void AddMoneyParameter(this NpgsqlCommand cmd, String parameterName, decimal parameterValue)
        {
            NpgsqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.NpgsqlDbType = NpgsqlDbType.Money;
            prm.Value = DataInputValidator.CheckInputDecimalValue(parameterValue);
            cmd.Parameters.Add(prm);

        }

        public static void AddTextParameter(this NpgsqlCommand cmd, String parameterName, String parameterValue)
        {
            NpgsqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.NpgsqlDbType = NpgsqlDbType.Text;
            prm.Value = DataInputValidator.CheckInputStringValue(parameterValue);
            cmd.Parameters.Add(prm);

        }
        









    }
}
